/**
 * 
 */
package ro.utcn.tp.taskManagement.gui;

import java.awt.Container;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;

import ro.utcn.tp.taskManagement.business.ImageService;
import ro.utcn.tp.taskManagement.business.UsersManager;
import ro.utcn.tp.taskManagement.model.ImageDefs;
import ro.utcn.tp.taskManagement.model.ImagePanel;
import ro.utcn.tp.taskManagement.model.User;

/**
 * @author Coni
 * 
 */
public class LoginPage implements ActionListener {
	private JLabel jlbTitlu;

	private JLabel jlbUsername;

	private JLabel jlbPassword;

	private JTextField jtfUsername;

	private JTextField jtfPassword;

	private JButton jbnLogIn;

	private JButton jbnCreateNewUser;

	private JButton jbnExit;

	private Container container;

	/** A reference to the image service. */

	private ImageService imageService;// pentru a tine imaginile

	private static LoginPage instance;

	/**
	 * The constructor of the class. initializes the screen.
	 */
	private LoginPage() {
		imageService = ImageService.getInstance();// instantiez imageservice
		createGUI();
	}

	public void createGUI() {

		container = new ImagePanel(
				imageService.getImage(ImageDefs.APPLICATION_BACKGROUND_PATH));
		container.setLayout(new GridBagLayout());
		arrangeComponents();
	}

	public void arrangeComponents() {
		jlbTitlu = new JLabel("Log in page");

		jlbUsername = new JLabel("Username:");
		jtfUsername = new JTextField("coni");
		jtfUsername.setEditable(true);

		jlbPassword = new JLabel("Password:");
		jtfPassword = new JPasswordField("coni");
		jtfPassword.setEditable(true);

		jbnLogIn = new JButton("Log in");
		jbnExit = new JButton("Exit application");
		jbnCreateNewUser = new JButton("Create new user");

		Font font = new Font("Times New Roman", Font.ITALIC, 30);
		jlbTitlu.setFont(font);

		GridBagConstraints gridBagConstraintsx01 = new GridBagConstraints();
		gridBagConstraintsx01.gridx = 0;
		gridBagConstraintsx01.gridy = 0;
		gridBagConstraintsx01.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx01.gridwidth = 2;// latimea e de 2 patratele din wigh
		gridBagConstraintsx01.fill = GridBagConstraints.BOTH;
		container.add(jlbTitlu, gridBagConstraintsx01);

		GridBagConstraints gridBagConstraintsx02 = new GridBagConstraints();
		gridBagConstraintsx02.gridx = 0;
		gridBagConstraintsx02.gridy = 1;
		gridBagConstraintsx02.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx02.gridwidth = 1;// latimea e de 1 patratele din wigh
		gridBagConstraintsx02.anchor = GridBagConstraints.LINE_START;
		container.add(jlbUsername, gridBagConstraintsx02);

		GridBagConstraints gridBagConstraintsx03 = new GridBagConstraints();
		gridBagConstraintsx03.gridx = 1;
		gridBagConstraintsx03.gridy = 1;
		gridBagConstraintsx03.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx03.gridwidth = 1;
		gridBagConstraintsx03.gridheight = 1;
		gridBagConstraintsx03.fill = GridBagConstraints.BOTH;
		container.add(jtfUsername, gridBagConstraintsx03);

		GridBagConstraints gridBagConstraintsx04 = new GridBagConstraints();
		gridBagConstraintsx04.gridx = 0;
		gridBagConstraintsx04.gridy = 2;
		gridBagConstraintsx04.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx04.gridwidth = 1;// latimea e de 1 patratele din wigh
		gridBagConstraintsx04.anchor = GridBagConstraints.WEST;
		container.add(jlbPassword, gridBagConstraintsx04);

		GridBagConstraints gridBagConstraintsx05 = new GridBagConstraints();
		gridBagConstraintsx05.gridx = 1;
		gridBagConstraintsx05.gridy = 2;
		gridBagConstraintsx05.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx05.gridwidth = 1;
		gridBagConstraintsx05.gridheight = 1;
		gridBagConstraintsx05.fill = GridBagConstraints.BOTH;
		container.add(jtfPassword, gridBagConstraintsx05);

		GridBagConstraints gridBagConstraintsx06 = new GridBagConstraints();
		gridBagConstraintsx06.gridx = 1;
		gridBagConstraintsx06.gridy = 3;
		gridBagConstraintsx06.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx06.gridwidth = 1;// latimea e de 1 patratele din wigh
		gridBagConstraintsx06.anchor = GridBagConstraints.WEST;
		container.add(jbnLogIn, gridBagConstraintsx06);

		GridBagConstraints gridBagConstraintsx08 = new GridBagConstraints();
		gridBagConstraintsx08.gridx = 1;
		gridBagConstraintsx08.gridy = 4;
		gridBagConstraintsx08.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx08.gridwidth = 1;// latimea e de 1 patratele din wigh
		gridBagConstraintsx08.anchor = GridBagConstraints.WEST;
		container.add(jbnCreateNewUser, gridBagConstraintsx08);

		GridBagConstraints gridBagConstraintsx07 = new GridBagConstraints();
		gridBagConstraintsx07.gridx = 1;
		gridBagConstraintsx07.gridy = 5;
		gridBagConstraintsx07.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx07.gridwidth = 1;
		gridBagConstraintsx07.gridheight = 1;
		gridBagConstraintsx07.fill = GridBagConstraints.BOTH;
		container.add(jbnExit, gridBagConstraintsx07);

		jbnLogIn.setEnabled(true);
		jbnExit.setEnabled(true);
		jbnCreateNewUser.setEnabled(true);
		jbnLogIn.addActionListener(this);
		jbnExit.addActionListener(this);
		jbnCreateNewUser.addActionListener(this);
		
		jbnCreateNewUser.setToolTipText("Create new user account");
		jbnLogIn.setToolTipText("Log into your existing user account");

	}

	public void stateChanged(ChangeEvent arg0) {

	}

	public static LoginPage getNewInstance() {
		instance = new LoginPage();
		return instance;
	}

	public void actionPerformed(ActionEvent e) {

		try {
			if (e.getSource() == jbnLogIn) {
				logIn();
			} else if (e.getSource() == jbnCreateNewUser) {
				createNewUser();
			} else {
				exit();
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	private void createNewUser() {
		ApplicationScreen.getInstance().setContainer(
				CreateUserPage.getNewInstance().getContainer());
	}

	private void exit() {
		System.exit(0);

	}

	public void logIn() {
		String username = jtfUsername.getText();
		String password = jtfPassword.getText();
		if (username.isEmpty()) {
			// afiseaza mesaj ca username nu a fost introdus
			JOptionPane.showMessageDialog(ApplicationScreen.getInstance()
					.getFrame(), "Va rugam introduceti nume utilizator!",
					"Data Input Error", JOptionPane.ERROR_MESSAGE);
			return;
		}
		if (password.isEmpty()) {
			// afiseaza mesaj ca parola nu a fost introdusa
			JOptionPane.showMessageDialog(ApplicationScreen.getInstance()
					.getFrame(), "Va rugam introduceti parola!",
					"Data Input Error", JOptionPane.ERROR_MESSAGE);
			return;
		}
		User user = UsersManager.getInstance().getUser(username, password);
		if(user==null){
			JOptionPane.showMessageDialog(ApplicationScreen.getInstance()
					.getFrame(), "Credentialele introduse sunt gresite!",
					"Data Input Error", JOptionPane.ERROR_MESSAGE);
			return;
		}
		if (user.getRole().equals(User.ADMIN_ROLE)) {
			//ApplicationScreen.getInstance().setContainer(
					//DirectoryChooserPage.getInstance(user).getContainer());

		} else {

			ApplicationScreen.getInstance().setContainer(
					UserPage.getNewInstance(user).getContainer());
		}
	}

	/**
	 * @return
	 */
	public Container getContainer() {
		return container;
	}

}
